package com.cqut.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.service.IProcessDetailService;
import com.cqut.dto.ProcessDetailDTO;
import com.cqut.model.ComProcess;
import com.cqut.model.ProcessDetail;
import com.cqut.dto.ComProcessDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.util.ResponseUtil;


@RestController
@RequestMapping("/api/processDetail")
public class ProcessDetailController {
    @Resource(name="processDetailService")
    private IProcessDetailService processDetailService;
    
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> processDetailList = processDetailService.loadMaps(null, null, null, null, limitShowDTO.getCurPage(), limitShowDTO.getLimit());
        long count = processDetailService.count();

        result.put("rows", processDetailList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    @RequestMapping(value = "/processList", method = RequestMethod.GET)
    public ResponseEntity<String> processList(
    		@RequestParam("comProcessId") String comProcessId,
    		@RequestParam("processName") String processName,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> processDetailList = processDetailService.findProcessDetail(comProcessId, processName, limitShowDTO);
        long count = processDetailService.countProcessDetail(comProcessId, processName);
        System.out.println(processDetailList);
        result.put("rows", processDetailList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @RequestMapping(value = "/{processDetailId}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("processDetailId") String processDetailId) {
        Map<String, Object> processDetailModel = processDetailService.findMap(processDetailId);

        return ResponseUtil.getResEntityForGetAndJson(processDetailModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        ProcessDetailDTO processDetailDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        ProcessDetail processDetailModel = processDetailDTO.toModel();

        return ResponseUtil.getResEntityForPPP(processDetailService.save(processDetailModel));
    }

    @RequestMapping(value = "/{processDetailId}", method = RequestMethod.PUT)
    public ResponseEntity<String> update(@PathVariable("processDetailId") String processDetailId,
        @Validated
        ProcessDetailDTO processDetailDTO, BindingResult errors) {

        ProcessDetail processDetailModel = processDetailDTO.toModel();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        processDetailModel.setWorksStartTime(sdf.format(new Date(Long.valueOf(processDetailModel.getWorksStartTime()))));
        processDetailModel.setWorksEndTime(sdf.format(new Date(Long.valueOf(processDetailModel.getWorksEndTime()))));
        processDetailModel.setGameTime(sdf.format(new Date(Long.valueOf(processDetailModel.getGameTime()))));
        processDetailModel.setProcessDetailId(processDetailId);
        System.out.println(processDetailModel.getProcessName() + " " + processDetailModel.getWorksStartTime());
        return ResponseUtil.getResEntityForPPP(processDetailService.update(processDetailModel));
    }

    @RequestMapping(value = "/{processDetailId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("processDetailId") String processDetailId) {
        return ResponseUtil.getResEntityForDel(processDetailService.delete(processDetailId));
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
        return ResponseUtil.getResEntityForDel(processDetailService.batchDelete(primaryKeys));
    }
}
